package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.service.FileUploadService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.atguigu.gmall.common.util.DateUtil;


import java.io.InputStream;
import java.util.Date;
import java.util.UUID;


@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Override
    public String uploadToMinio(MultipartFile file) throws Exception {
        String policy = "{\n" +
                "  \"Version\": \"2012-10-17\",\n" +
                "  \"Statement\": [\n" +
                "    {\n" +
                "      \"Effect\": \"Allow\",\n" +
                "      \"Principal\": {\n" +
                "        \"AWS\": [\n" +
                "          \"*\"\n" +
                "        ]\n" +
                "      },\n" +
                "      \"Action\": [\n" +
                "        \"s3:GetBucketLocation\",\n" +
                "        \"s3:ListBucket\",\n" +
                "        \"s3:ListBucketMultipartUploads\"\n" +
                "      ],\n" +
                "      \"Resource\": [\n" +
                "        \"arn:aws:s3:::mall\"\n" +
                "      ]\n" +
                "    },\n" +
                "    {\n" +
                "      \"Effect\": \"Allow\",\n" +
                "      \"Principal\": {\n" +
                "        \"AWS\": [\n" +
                "          \"*\"\n" +
                "        ]\n" +
                "      },\n" +
                "      \"Action\": [\n" +
                "        \"s3:GetObject\",\n" +
                "        \"s3:ListMultipartUploadParts\",\n" +
                "        \"s3:PutObject\",\n" +
                "        \"s3:AbortMultipartUpload\",\n" +
                "        \"s3:DeleteObject\"\n" +
                "      ],\n" +
                "      \"Resource\": [\n" +
                "        \"arn:aws:s3:::mall/*\"\n" +
                "      ]\n" +
                "    }\n" +
                "  ]\n" +
                "}";
        //1）抽取到配置文件中，让代码读取配置文件的值
        //2）如何让任何代码直接可以 @Autowired的方式拿到MinioClient 而不是自己new

        //1、创建出MinioClient
        MinioClient minioClient = new MinioClient("http://192.168.197.100:9000",
                "admin",
                "admin123456");

        //2、判断桶是否存在
        if(!minioClient.bucketExists("mall")){
            minioClient.makeBucket("mall");
            minioClient.setBucketPolicy("mall",policy);
        }

        //3、上传文件
        String date = DateUtil.formatDate(new Date());

        String objectName = date+"/"+UUID.randomUUID().toString().replace("-","")
                +  "_"
                + file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        //参数
        PutObjectOptions options = new PutObjectOptions(inputStream.available(), -1);
        options.setContentType(file.getContentType());

        minioClient.putObject("mall",objectName,inputStream,options);

        //4、返回文件的访问地址
        String url = "http://192.168.197.100:9000/mall/"+objectName;



        return url;
    }
}
